<template>
  <div class="vue-city-backdrop" v-show="value">
    <div class="city-wrapper">
      <i class="iconfont iconbtn-close" @click="onClose"></i>
      <div class="city-content">
        <vue-tab :tabtitles="titles" :isTitleFixed="true" @index="onPage" :curPage="currentPage" ref="tab">
          <div class="tab-content-container">
            <div v-show="currentPage == 1">
              <div class="city-item" :class="{active: province === item.name}" v-for="item in allCityList" :key="item.code" @click="onCity(item)">
                <span>{{item.name}}</span>
                <i class="iconfont icongc-btn-skip"></i>
              </div>
            </div>
          </div>
          <div class="tab-content-container">
            <div v-show="currentPage == 2">
              <div class="city-item" :class="{active: city === item.name}" v-for="item in cityList" :key="item.code" @click="onArea(item)">
                <span>{{item.name}}</span>
                <i class="iconfont icongc-btn-skip"></i>
              </div>
            </div>
          </div>
          <div class="tab-content-container">
            <div v-show="currentPage == 3">
              <div class="city-item" :class="{active: area === item.name}" v-for="item in areaList" :key="item.code" @click="onAddress(item)">
                <span>{{item.name}}</span>
                <i class="iconfont icongc-btn-skip"></i>
              </div>
            </div>
          </div>
        </vue-tab>
      </div>
    </div>
  </div>
</template>
<script>
import cityData from '@/common/city.json'
import VueTab from '@/components/VueTab'
import scroll from '@/mixins/scroll'
export default {
  mixins: [scroll],
  components: {
    VueTab
  },
  props: {
    value: {
      type: Boolean,
      default: false,
    },
    infoData: {
      type: Object
    }
  },
  data() {
    return {
      allCityList: cityData,
      cityList: [],
      areaList: [],
      titles: ['请选择'],
      currentPage: 1,
      province: '',
      city: '',
      area: '',
      addressObj: {}
    }
  },
  watch: {
    infoData: {
      handler: function () {
        this.initCity()
      },
      deep: true,
    },
    value(newVal) {
      if (newVal) {
        this.scroll(true)
      } else {
        this.scroll(false)
      }
    },
  },
  methods: {
    onCity(item) {
      if (this.cityList.length <= 0) {
        this.$set(this.titles, 1, '请选择')
      }
      if (this.province !== item.name) {
        this.titles.length = 1
        this.$set(this.titles, 1, '请选择')
        this.cityList = []
      }
      this.province = item.name
      this.addressObj['province'] = this.province
      this.$nextTick(() => {
        this.$set(this.titles, 0, item.name)
        this.cityList = item.children
        this.$refs.tab.next()
      })
    },
    onArea(item) {
      if (this.areaList.length <= 0) {
        this.$set(this.titles, 1, '请选择')
      }
      if (this.city !== item.name) {
        this.$set(this.titles, 2, '请选择')
        this.areaList = []
      }
      this.city = item.name
      this.addressObj['city'] = this.city
      this.$nextTick(() => {
        this.$set(this.titles, 1, item.name)
        this.areaList = item.children
        this.$refs.tab.next()
      })
    },
    onAddress(item) {
      this.area = item.name
      this.addressObj['area'] = this.area
      this.$nextTick(() => {
        this.$set(this.titles, 2, item.name)
      })
      this.$emit('change', JSON.stringify(this.addressObj))
    },
    onPage(index) {
      this.currentPage = index
    },
    onClose() {
      this.currentPage = 1
      this.titles.length = 1
      this.province = ''
      this.city = ''
      this.area = ''
      this.cityList = []
      this.areaList = []
      this.$set(this.titles, 0, '请选择')
      this.$refs.tab.setPage(1)
      this.$nextTick(() => {
        this.$emit('close')
      })
    },
    initCity() {
      this.province = this.infoData.province
      // 遍历city 数据
      cityData.forEach((item) => {
        // 省份名字 和 回传省份名字一致
        this.city = this.infoData.city
        if (item.name === this.infoData.province) {
          // 展示当前省份名字 取 index, 并且下面的市级数据赋予  this.cityItems[1].values
          this.cityList = item.children
          this.area = this.infoData.area
          // 遍历市级数据
          item.children.forEach((item1) => {
            // 展示当面市级名字 取 index, 并且下面的县区级数据赋予  this.cityItems[2].values
            if (item1.name === this.infoData.city) {
              this.areaList = item1.children
            }
          })
        }
      })
      this.$nextTick(() => {
        this.$set(this.titles, 0, this.province)
        this.$set(this.titles, 1, this.city)
        this.$set(this.titles, 2, this.area)
      })
    }
  }
}
</script>
<style lang="scss">
.vue-city-backdrop {
  .tab-title-nav {
    background-color: #fff;
  }
  .tab-title-container {
    justify-content: flex-start;
  }
  .tab-title {
    flex: none;
    padding: 0 0.2rem;
    color: #000;
    &.active {
      color: #2460e5;
      span {
        width: 60%;
        height: 0.05rem;
        background-color: #2460e5;
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
        margin: auto;
      }
    }
  }
}
</style>
<style lang="scss" scoped>
.vue-city-backdrop {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.7);
  z-index: 999;
  .city-wrapper {
    position: absolute;
    left: 0;
    bottom: 0;
    width: 100%;
    background: transparent;
    transform: translate3d(0px, 0px, 0px);
    z-index: 9999;
    .iconbtn-close {
      font-size: 0.32rem;
      position: absolute;
      right: 0.24rem;
      top: 0.2rem;
      z-index: 9999;
    }
    .city-content {
      position: relative;
      width: 100%;
      height: 8rem;
      background: white;
      overflow: hidden;
      overflow-y: auto;
      .city-item {
        width: 100%;
        line-height: 0.64rem;
        border-bottom: 1px solid #f5f5f5;
        margin-top: 0.14rem;
        font-size: 0.26rem;
        display: inline-flex;
        align-items: center;
        justify-content: space-between;
        padding: 0 0.24rem;
        &.active {
          color: #2460e5;
        }
        .icongc-btn-skip {
          font-size: 0.24rem;
          color: #d9d9d9;
        }
      }
    }
  }
}
</style>